package com.neu.flashmall.product.controller;

import com.neu.common.utils.Result;
import com.neu.common.utils.ValidUtil;
import com.neu.common.valid.AddGroup;
import com.neu.common.valid.UpdateGroup;
import com.neu.flashmall.product.entity.PmsAttrGroup;
import com.neu.flashmall.product.service.PmsAttrGroupService;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api("这是一个属性组操作接口")
@RequestMapping(value = "/pms_attrGroup")
public class PmsAttrGroupController {

    @Resource
    private PmsAttrGroupService pmsAttrGroupService;

    @PutMapping("/check/addAttrGroup")
    public Result addAttrGroup(
            @RequestBody @Validated(AddGroup.class) PmsAttrGroup pmsAttrGroup,
            BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ValidUtil.createResultByBindingResult(bindingResult);
        }
        String outcome = pmsAttrGroupService.add(pmsAttrGroup);
        if("添加成功".equals(outcome)){
            return Result.ok().setMessage(outcome).put(pmsAttrGroup.getId());
        }
        return Result.error().setMessage(outcome);
    }

    @PostMapping("/check/updateAttrGroup")
    public Result updateAttrGroup(
            @RequestBody @Validated(UpdateGroup.class) PmsAttrGroup pmsAttrGroup,
            BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ValidUtil.createResultByBindingResult(bindingResult);
        }
        String result = this.pmsAttrGroupService.update(pmsAttrGroup);
        if("更新成功".equals(result)){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @GetMapping("/getAllAttrGroup")
    public Result getAllAttrGroup(){
        List<PmsAttrGroup> result = this.pmsAttrGroupService.getAllAttrGroup();
        return Result.ok().put(result);
    }

    @DeleteMapping("/check/deleteAttrGroup")
    public Result deleteAttrGroup(Long id){
        String result = this.pmsAttrGroupService.delete(id);
        if("删除成功".equals(result)){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @GetMapping(value = "/getOneById")
    public Result getOneById(Long id){
        PmsAttrGroup result = this.pmsAttrGroupService.getOneById(id);
        return Result.ok().put(result);
    }
}
